﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "GnufElementCanBePickedInModifyAction.h"
#include "IElement.h"
#include "IElementBasicInformation.h"
#include "ILinkElementProxy.h"
#include "UniIdentity.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace gfam;

ElementCanBePickedInModifyAction::ElementCanBePickedInModifyAction()
    : m_isEnabled(true)
{
}

bool ElementCanBePickedInModifyAction::CanBePicked(const IElement * pElement) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"输入的Element指针为空，不合法", L"paramodel", L"2024-02-20");

    const IElementBasicInformation * pElementBasicInformation = pElement->GetBasicInformation();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElementBasicInformation, L"输入的pElementBasicInformation指针为空，不合法", L"paramodel", L"2024-02-20");
    const UniIdentity & category = pElementBasicInformation->GetCategoryUid();
    if (m_categoriesCanNotBePicked.find(category) == m_categoriesCanNotBePicked.end())
    {
        const ILinkElementProxy * pLinkElement = quick_cast<const ILinkElementProxy>(pElement);
        if (pLinkElement != nullptr)
        {
            /*std::wstring sStandardCode = std::move(LinkElementUtils::GetLinkedElementStandardCode(pElement));
            if (!sStandardCode.empty() && m_standardCodesCannotBePicked.find(sStandardCode) != m_standardCodesCannotBePicked.end())
            {
                return false;
            }*/
            return true;
        }

    // 如果强制不能被pick，且不能被UI进行操作改变"可拾取性"直接返回
    return m_categoriesDefaultCannotBePicked.find(category) == m_categoriesDefaultCannotBePicked.end() && m_categoriesCanNotBePicked.find(category) == m_categoriesCanNotBePicked.end();
    }
    return false;
}

bool ElementCanBePickedInModifyAction::IsEnabled() const
{
    return m_isEnabled; 
}

bool ElementCanBePickedInModifyAction::SetIsEnabled(bool isEnabled)
{
    m_isEnabled = isEnabled; 
    return true;
}

OwnerPtr<IElementCanBePickedInModifyAction> ElementCanBePickedInModifyAction::Clone() const
{
    OwnerPtr<ElementCanBePickedInModifyAction> opElementCanBePickedInModifyAction = NEW_AS_OWNER_PTR(ElementCanBePickedInModifyAction);
    opElementCanBePickedInModifyAction->m_isEnabled = this->m_isEnabled;
    opElementCanBePickedInModifyAction->m_categoriesCanBePicked = this->m_categoriesCanBePicked;
    opElementCanBePickedInModifyAction->m_categoriesCanNotBePicked = this->m_categoriesCanNotBePicked;
    opElementCanBePickedInModifyAction->m_standardCodesCannotBePicked = this->m_standardCodesCannotBePicked;
    return TransferOwnershipCast<IElementCanBePickedInModifyAction>(opElementCanBePickedInModifyAction);
}

void ElementCanBePickedInModifyAction::InitCategories(const std::vector<UniIdentity>& categoriesIds)
{
    m_categoriesCanBePicked.clear();
    m_categoriesCanNotBePicked.clear();
    FOR_EACH(id, categoriesIds)
    {
        m_categoriesCanBePicked.insert(id);
    }
}

void ElementCanBePickedInModifyAction::InitDefaultCannotBePickedCategories(const std::vector<gcmp::UniIdentity> & categoriesIds)
{
    m_categoriesDefaultCannotBePicked.clear();
    FOR_EACH(id, categoriesIds)
    {
        m_categoriesDefaultCannotBePicked.insert(id);
    }
}

void ElementCanBePickedInModifyAction::ClearCategories(const std::vector<UniIdentity>& categoriesIds)
{
    m_categoriesCanBePicked.clear();
    m_categoriesCanNotBePicked.clear();
    FOR_EACH(id, categoriesIds)
    {
        m_categoriesCanNotBePicked.insert(id);
    }
}

void ElementCanBePickedInModifyAction::SetCanBePickedCategory(const UniIdentity & uid)
{
    if (uid.IsValid())
    {
        //如果没有，加上
        auto itor1 = m_categoriesCanBePicked.find(uid);
        if (itor1 == m_categoriesCanBePicked.end())
        {
            m_categoriesCanBePicked.insert(uid);
        }

        //如果有，移除
        auto itor2 = m_categoriesCanNotBePicked.find(uid);
        if (itor2 != m_categoriesCanNotBePicked.end())
        {
            m_categoriesCanNotBePicked.erase(itor2->GetGuid());
        }
    }
}

void ElementCanBePickedInModifyAction::SetCanNotBePickedCategory(const UniIdentity & uid)
{
    if (uid.IsValid())
    {
        //如果有，移除
        auto iter1 = m_categoriesCanBePicked.find(uid);
        if (iter1 != m_categoriesCanBePicked.end())
        {
            m_categoriesCanBePicked.erase(iter1->GetGuid());
        }

        //如果没有，加上
        auto iter2 = m_categoriesCanNotBePicked.find(uid);
        if (iter2 == m_categoriesCanNotBePicked.end())
        {
            m_categoriesCanNotBePicked.insert(uid);
        }
    }
}

void gfam::ElementCanBePickedInModifyAction::SetCanBePickedStandardCode(const std::wstring & standardCode)
{
    m_standardCodesCannotBePicked.erase(standardCode);
}

void gfam::ElementCanBePickedInModifyAction::SetCanNotBePickedStandardCode(const std::wstring & standardCode)
{
    m_standardCodesCannotBePicked.insert(standardCode);
}
